﻿using RTS.Lib.Comm.ADSBLL;
using RTS.Lib.Comm.OPCBLL;
using RTS.Lib.Comm.OPCModel;
using RTS.Lib.Comm.S7BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ControlTest
{
    public partial class Frm_WriteValue : Form
    {
        public Group MyGroup { get; set; }
        public Item MyItem { get; set; }
        public Frm_WriteValue(Group group, Item item)
        {
            MyGroup = group;
            MyItem = item;
            InitializeComponent();
            textBox_ItemName.Text = item.ItemName;
            switch (item.DriveType)
            {
                case DriveType.S7:
                    textBox_dataType.Text = ((RTSS7Tag)item.ActualTag).DataType.ToString();
                    break;
                case DriveType.ADS:
                    textBox_dataType.Text = ((RTSADSTag)item.ActualTag).DataType.ToString();
                    break;
                default:
                    throw new Exception($"Frm_WriteValue中驱动类型“{item.DriveType}”不存在");
            }
        }
        private void button_Confirm_Click(object sender, EventArgs e)
        {
            ItemValue itemValue = new ItemValue(MyItem);
            itemValue.Value = CovertToDotNet(MyItem, textBox_writeValue.Text);
            ItemResult[] itemResults1 = MyGroup.Write(new ItemValue[] { itemValue });
            this.Close();
            this.Dispose();
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
        private object CovertToDotNet(Item item, string value)
        {
            object result = null;
            DriveType driveType = item.DriveType;
            switch (driveType)
            {
                case DriveType.S7:
                    {
                        RTS.Lib.Comm.S7Model.DataType datatype = ((RTSS7Tag)item.ActualTag).DataType;
                        switch (datatype)
                        {
                            case RTS.Lib.Comm.S7Model.DataType.Bool:
                                if (value == "1")
                                    result = true;
                                else if (value == "0")
                                    result = false;
                                else
                                    result = Convert.ToBoolean(value);
                                break;
                            case RTS.Lib.Comm.S7Model.DataType.Byte:
                                result = Convert.ToByte(value);
                                break;
                            case RTS.Lib.Comm.S7Model.DataType.SInt:
                            case RTS.Lib.Comm.S7Model.DataType.Int:
                                result = Convert.ToInt16(value);
                                break;
                            case RTS.Lib.Comm.S7Model.DataType.DInt:
                                result = Convert.ToInt32(value);
                                break;
                            case RTS.Lib.Comm.S7Model.DataType.Word:
                                result = Convert.ToUInt16(value);
                                break;
                            case RTS.Lib.Comm.S7Model.DataType.DWord:
                                result = Convert.ToUInt32(value);
                                break;
                            case RTS.Lib.Comm.S7Model.DataType.Real:
                                result = Convert.ToSingle(value);
                                break;
                            case RTS.Lib.Comm.S7Model.DataType.String:
                            case RTS.Lib.Comm.S7Model.DataType.Array_Of_Char:
                                result = value.ToString();
                                break;
                            case RTS.Lib.Comm.S7Model.DataType.Date:
                                result = Convert.ToDateTime(value);
                                break;
                            case RTS.Lib.Comm.S7Model.DataType.Time:
                                result = Convert.ToInt32(value);
                                break;
                            case RTS.Lib.Comm.S7Model.DataType.Time_Of_Day:
                                result = Convert.ToDateTime(value);
                                break;
                            default:
                                throw new Exception($"CovertToDotNet中变量类型“{datatype}”不存在"); ;
                        }
                    }

                    break;
                case DriveType.ADS:
                    {
                        RTS.Lib.Comm.ADSModel.DataType datatype = ((RTSADSTag)item.ActualTag).DataType;
                        switch (datatype)
                        {
                            case RTS.Lib.Comm.ADSModel.DataType.BOOL:
                                result = Convert.ToBoolean(value);
                                break;
                            case RTS.Lib.Comm.ADSModel.DataType.BYTE:
                                result = Convert.ToBoolean(value);
                                break;
                            case RTS.Lib.Comm.ADSModel.DataType.INT:
                                result = Convert.ToInt16(value);
                                break;
                            case RTS.Lib.Comm.ADSModel.DataType.DINT:
                                result = Convert.ToInt32(value);
                                break;
                            case RTS.Lib.Comm.ADSModel.DataType.REAL:
                                result = Convert.ToSingle(value);
                                break;
                            case RTS.Lib.Comm.ADSModel.DataType.STRING:
                                result = value.ToString();
                                break;
                            case RTS.Lib.Comm.ADSModel.DataType.DATE:
                            case RTS.Lib.Comm.ADSModel.DataType.DATE_AND_TIME:
                                result = Convert.ToDateTime(value);
                                break;
                            case RTS.Lib.Comm.ADSModel.DataType.TIME:
                                result = TimeSpan.Parse(value);
                                break;
                            case RTS.Lib.Comm.ADSModel.DataType.TIME_OF_DAY:
                                result = Convert.ToDateTime(value).TimeOfDay;
                                break;
                            default:
                                throw new Exception($"CovertToDotNet中变量类型“{datatype}”不存在"); ;
                        }
                    }
                    break;
                default:
                    throw new Exception($"CovertToDotNet中驱动类型“{driveType}”不存在");

            }
            return result;
        }

    }
}
